<?php

class openapi_object {

    private static $_class = null;

    private static $_method = null;

    private static $_path = null;

    private static $_version = 1;

    private static $_flag = null;

    private static $_type = 'json';

    private static $_charset = 'utf-8';

    private static $_appParams = array();

    private static $_appParamsFormat = array();

    private static $_funcObject = null;

    private static $_paramsObject = null;

    private static $_templateObject = null;

    private static $_conf = null;

    private static $_task_id = '';

    private static $_original_data = null;

    private static $_original_num = null;

    private static $_output_data = null;

    public function instance($sysParams, $appParams, &$code, &$sub_msg) {
        
        // 设置系统参数
        $this->setSysParams($sysParams);
        
        // 加载接口类
        if (! $this->loadModule()) {
            $code = 'e000005';
            return false;
        }
        
        // 加载接口参数类
        if (! $this->loadParamModule()) {
            $code = 'e000005';
            return false;
        }
        
        // 设置应用参数
        if (! $this->setAppParams($appParams, $sub_msg)) {
            $code = 'e000006';
            return false;
        }
        
        // 加载接口配置信息
        if (! $this->loadTemplateModule()) {
            $code = 'e000007';
        }
        
        return true;
    }

    private function setSysParams($params) {
        self::$_flag = $params['flag'];
        self::$_class = $params['class'];
        self::$_method = $params['method'];
        self::$_path = $params['path'];
        self::$_version = $params['ver'];
        self::$_type = $params['type'];
        self::$_charset = $params['charset'];
        self::$_task_id = $params['task_id'];
    }

    private function loadModule() {
        if (isset(self::$_funcObject[self::$_flag])) {
            return self::$_funcObject[self::$_flag];
        } else {
            if (self::$_path) {
                $className = sprintf("openapi_api_function_v%s_%s_%s", self::$_version, self::$_path, self::$_class);
            } else {
                $className = sprintf("openapi_api_function_v%s_%s", self::$_version, self::$_class);
            }
            $obj = new $className();
            if (is_object($obj) && method_exists($obj, self::$_method)) {
                self::$_funcObject[self::$_flag] = $obj;
                return true;
            } else {
                return false;
            }
        }
    }

    private function loadParamModule() {
        if (isset(self::$_paramsObject[self::$_flag])) {
            return self::$_paramsObject[self::$_flag];
        } else {
            if (self::$_path) {
                $className = sprintf("openapi_api_params_v%s_%s_%s", self::$_version, self::$_path, self::$_class);
            } else {
                $className = sprintf("openapi_api_params_v%s_%s", self::$_version, self::$_class);
            }
            $obj = new $className();
            if (is_object($obj)) {
                self::$_paramsObject[self::$_flag] = $obj;
                return true;
            } else {
                return false;
            }
        }
    }

    public function setAppParams($params, &$sub_msg) {
        if ($this->checkParams($params, $sub_msg)) {
            self::$_appParams = $params;
            $this->formartAppParams();
            return true;
        } else {
            return false;
        }
    }

    private function checkParams($params, &$sub_msg) {
        return self::$_paramsObject[self::$_flag]->checkParams(self::$_method, $params, $sub_msg);
    }

    private function formartAppParams() {
        $appParmas = self::$_paramsObject[self::$_flag]->getAppParams(self::$_method);
        foreach ($appParmas as $defined_param => $attribute) {
            if ($attribute['type'] == 'json') {
                // 已经被解析过的json不再处理，modefied by hmg
                $fData[$defined_param] = is_array(self::$_appParams[$defined_param]) ? self::$_appParams[$defined_param] : json_decode(self::$_appParams[$defined_param], 1);
            } else {
                $fData[$defined_param] = self::$_appParams[$defined_param];
            }
        }
        // 获取接口原始单据号
        $appParmasKey = self::$_paramsObject[self::$_flag]->getAppParamsKey(self::$_method);
        if (isset($fData['content'][$appParmasKey['key']])) {
            self::$_original_num = $fData['content'][$appParmasKey['key']];
        }
        $fData['task_id'] = self::$_task_id;
        self::$_appParamsFormat = $fData;
    }

    private function loadTemplateModule() {
        
        // 加载标识的配置信息
        $this->loadConf();
        
        // 根据配置信息识别调用的输出模板类to do $_templateObject[self::$_flag]
    }

    private function loadConf() {
        self::$_conf = openapi_setting::getConf(self::$_flag);
    }

    public function process(&$result, &$code, &$sub_msg) {
        // 输入参数获取原始数据
        $this->input($code, $sub_msg);
        
        // 判断应用级处理结果是否成功
        if (self::$_original_data === false) {
            return false;
        }
        
        // 按照模板或定义调整输出数据
        $this->output();
        
        $result = self::$_output_data;
        return true;
    }

    private function input(&$code, &$sub_msg) {
        self::$_original_data = self::$_funcObject[self::$_flag]->{self::$_method}(self::$_appParamsFormat, $code, $sub_msg);
        if (self::$_original_data) {
            kernel::single('openapi_log')->response(self::$_class . '.' . self::$_method, self::$_task_id, self::$_appParamsFormat, self::$_original_data, self::$_flag, true, 'succ', self::$_original_num);
        } else {}
    }

    private function output() {
        if (isset(self::$_templateObject[self::$_flag])) {
            // 根据模板及定义调整输出to do
        } else {
            self::$_output_data = self::$_original_data;
        }
    }
}